package com.thl.levelcache.decoration.basic;


import com.thl.levelcache.decoration.AbstractDecorate;
import com.thl.levelcache.util.JvmMapCacheUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;


@Component
public class JvmComponentCache extends AbstractDecorate {


    @Override
    public <T> T getCache(String key, Class<T> returnClass, ProceedingJoinPoint joinPoint) {
        // 先查询我们的一级缓存（Jvm内置）
        T jvmCache = JvmMapCacheUtils.getEntity(key, returnClass);
        if (jvmCache != null) {
            return (T) jvmCache;
        }
        try {
            //查询目标对象方法
            Object resultDb = joinPoint.proceed();
            // 数据库DB有的情况 将该内容缓存到当前Jvm中
            JvmMapCacheUtils.putEntity(key, resultDb);
            return (T) resultDb;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return null;
        }
    }


}
